package com.lxw.station.task;

import com.alibaba.fastjson.JSONArray;
import com.lxw.station.api.CSDNApi;
import com.lxw.station.components.CsdnService;
import com.lxw.station.config.CSDNData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

//@Component
@Slf4j

public class CsdnTask4Cron {
    @Resource
    RedisTemplate<String, Object> redisTemplate;
    @Resource
    CsdnService csdnService;


    /**
     * 更新领域内容榜
     */
    @Scheduled(cron = "0 16 9 * * ?")
    public void updateContentRank() {
        csdnService.updateContentRank();
    }

    /**
     * 新晋作者
     */
    @Scheduled(cron = "0 26 9 * * ?")
    public void updateRankNewAuthor() {
        log.info("开始更更新晋作者榜");
        JSONArray jsonArray = CSDNApi.getNewAuthorFromCsdn();
        if (CollectionUtils.isEmpty(jsonArray)) {
            return;
        }
        redisTemplate.opsForValue().set(CSDNData.RANK_NEW_AUTHOR, jsonArray);
    }

    /**
     * 作者周榜
     */
    @Scheduled(cron = "0 36 9 ? * 1")
    public void updateRankWeeklyAuthor() {
        log.info("开始更更作者周榜");
        JSONArray jsonArray = CSDNApi.getRankWeeklyAuthorFromCsdn();
        if (CollectionUtils.isEmpty(jsonArray)) {
            return;
        }
        redisTemplate.opsForValue().set(CSDNData.RANK_WEEKLY_AUTHOR, jsonArray);
    }

    /**
     * 原力榜(总榜) 日更
     */
    @Scheduled(cron = "0 46 9 * * ?")
    public void updateMonthlyInfluenceAll() {
        log.info("开始更更原力榜(总榜) ");
        JSONArray jsonArray = CSDNApi.monthlyInfluence();
        if (CollectionUtils.isEmpty(jsonArray)) {
            return;
        }
        redisTemplate.opsForValue().set(CSDNData.RANK_WEEKLY_INFLUENCE_ALL, jsonArray);
    }

    /**
     * 原力榜(常驻地区) 日更
     */
    @Scheduled(cron = "0 56 9 * * ?")
    public void updateMonthlyInfluenceCity() {
        log.info("开始更更原力榜(常驻地区) ");
        JSONArray jsonArray = CSDNApi.monthlyInfluence4city();
        if (CollectionUtils.isEmpty(jsonArray)) {
            return;
        }
        redisTemplate.opsForValue().set(CSDNData.RANK_WEEKLY_INFLUENCE_CITY, jsonArray);

    }

    /**
     * 新人榜 日更
     */
    @Scheduled(cron = "0 6 10 * * ?")
    public void update_new_user_influence() {
        log.info("开始更更新人榜");
        JSONArray jsonArray = CSDNApi.new_user_influence();
        if (CollectionUtils.isEmpty(jsonArray)) {
            return;
        }
        redisTemplate.opsForValue().set(CSDNData.RANK_WEEKLY_NEW_USER_INFLUENCE, jsonArray);
    }

    /**
     * 关注评价点赞
     */
    @Scheduled(cron = "0 43 10 * * ?")
    public void followLikeComment() {
        log.info("开始关注评价点赞");
        csdnService.followLikeComment();
    }

}
